package org.hu.controller;

import lombok.extern.slf4j.Slf4j;
import org.hu.entity.pojo.Category;
import org.hu.entity.vo.Result;
import org.hu.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author HYH
 * &#064;date  2020/6/11 - 14:05
 */

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    // 添加分类
    @RequestMapping("/add")
    public Result<String> addCategory(String name) {
        log.info("新增分类:{}",name);
        categoryService.addCategory(name);
        return Result.success("添加成功");
    }

    // 删除一个分类
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteCategory(@PathVariable Integer id) {
        log.info("删除分类:{}",id);
        categoryService.deleteCategory(id);
        return Result.success("删除成功");
    }

    // 修改一个分类
    @PostMapping("/update")
    public Result<String> updateCategory(@RequestBody Category category) {
        log.info("修改分类:{}",category);
        categoryService.updateCategory(category);
        return Result.success("修改成功");
    }

    //查询所有分类
    @GetMapping("/list")
    public Result<List<Category>> listCategory() {
        log.info("查询所有分类");
        return Result.success("查询成功",categoryService.listCategory());
    }
}
